package jcl;

import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import static org.junit.Assert.assertTrue;

/**
 * Tests to see if MajorTom moves a random distance on each turn.
 * 
 * @author Jerry Lau
 * 
 */
public class TestMajorTomRandomMovement extends RobotTestBed {

  boolean distanceIsRandom = false;
  double distance = 0;
  double distance2 = 0;
  double x1;
  double y1;
  int firstTurnCounter = 0;

  /**
   * Gets the name of the robots which will battle.
   * 
   * @return The list of robots in the match.
   */
  public String getRobotNames() {
    return "sample.SittingDuck,jcl.MajorTom";
  }

  /**
   * Runs for 10 rounds.
   * 
   * @return Number of rounds
   */
  public int getNumRounds() {
    return 10;
  }

  /**
   * Checks if the robot moves the same distance twice consecutively.
   * 
   * @param event Battle info
   */
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];

    if (firstTurnCounter == 0) {
      x1 = robot.getX();
      y1 = robot.getY();
      firstTurnCounter = 1;
    }

    else {
      double x2 = robot.getX();
      double y2 = robot.getY();

      double lengthX = Math.abs(x1 - x2);
      double lengthY = Math.abs(y1 - y2);

      if (distance == 0) {
        distance = Math.hypot(lengthX, lengthY);
        x1 = x2;
        y1 = y2;
      }

      else if (distance2 == 0) {
        distance2 = Math.hypot(lengthX, lengthY);
      }
    } // end else

    if (distance != distance2) {
      distanceIsRandom = true;
    }

  }

  /**
   * After battle, check if the two consecutive distances were different and also prints the
   * distances that were compared.
   * 
   * @param event Completed battle info
   */
  public void onBattleCompleted(BattleCompletedEvent event) {
    System.out.println(distance + " is not equal to " + distance2);
    assertTrue("Check if distance was random...", distanceIsRandom);
  }

}